using System;
using BusinessLogic.Connectivity.Entities;
using BusinessLogic.Managers.Processing.RemittanceAdvice.Exceptions;
using DataAccess.Services;

namespace BusinessLogic.Managers.Processing.RemittanceAdvice
{
	internal class RemittanceAdviceProcessingExceptionCatcher : ProcessingExceptionCatcherBase
	{
		public override void Execute(Action<object, IServiceManager> func, object document, IServiceManager serviceManager, DocumentEnvelope envelope)
		{
			try
			{
				func(document, serviceManager);
			}
			catch (InvalidRemittanceAdviceNumberFormatException e)
			{
				envelope.RejectionReason =
					string.Format("'{0}' is not a correct number for Remittance Advice - it should be alpha-numeric.", e.DocumentNumber);
				throw;
			}
			catch (InvalidRemittanceAdviceNumberLengthException e)
			{
				envelope.RejectionReason =
					string.Format(
						"'{0}' is not a correct number for Remittance Advice - its length should be from 8 to 25 characters.",
						e.DocumentNumber);
				throw;
			}
			catch (InvalidRemittanceAdviceTotalAmountException)
			{
				envelope.RejectionReason = "The Remittance Advice sent had invalid total amount. The total amount must be positive.";
				throw;
			}
			catch (PayerAndBillToAccountCodesForRemittanceAdviceLeadToTheSameClientException)
			{
				envelope.RejectionReason = "BillTo and Payer accounts belong to the same client.";
				throw;
			}
			catch (RemittanceAdviceEmptyDescriptionException)
			{
				envelope.RejectionReason = "Remittance Advice must have a description";
				throw;
			}
		}
	}
}